package com.hwh.bootdemoweb.Netty第六章;


import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

import java.net.InetSocketAddress;

/**
 * @author wenhao hu
 * @title: HelloClient
 * @projectName bootdemoweb
 * @description: TODO
 * @date 2021/12/1515:41
 */
public class HelloClient {
    public static void main(String[] args) throws InterruptedException {
        //1.启动类
        new Bootstrap()
                //2. 添加event Group
                .group(new NioEventLoopGroup())
                //3. 选择客户端的channel实现
                .channel(NioSocketChannel.class)
                //4. 添加处理器
                .handler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        //添加具体handler
                        ch.pipeline().addLast(new StringEncoder()); //将bytebuffer转换成字符串
                    }
                })
                .connect(new InetSocketAddress(8080))
                .sync()
                .channel()
                //6.向服务器发送数据
                .writeAndFlush("hello world");
    }
}
